Use XMLRPC via HttpRequest-node to post on Wordpress.com

工作流概述

这是一个包含11个节点的复杂工作流,主要用于自动化处理各种任务。

工作流源代码

下载
{
  "id": "yPIST7l13huQEjY5",
  "meta": {
    "instanceId": "98bf0d6aef1dd8b7a752798121440fb171bf7686b95727fd617f43452393daa3",
    "templateCredsSetupCompleted": true
  },
  "name": "Use XMLRPC via HttpRequest-node to post on Wordpress.com",
  "tags": [
    {
      "id": "uumvgGHY5e6zEL7V",
      "name": "Published Template",
      "createdAt": "2025-02-10T11:18:10.923Z",
      "updatedAt": "2025-02-10T11:18:10.923Z"
    }
  ],
  "nodes": [
    {
      "id": "8a64ffca-804a-4793-a721-3cb670aec22f",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "position": [
        -380,
        -700
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "1be018c7-51fe-4ea2-967d-ce47a2e8795c",
              "name": "wordpressUrl",
              "type": "string",
              "value": "YOURBLOG.wordpress.com"
            },
            {
              "id": "95377f4f-184b-46a7-94c7-b2313c314cb2",
              "name": "wordpressUsername",
              "type": "string",
              "value": "YourUserName"
            },
            {
              "id": "fdc99dc6-d9b0-4d2f-b770-1d8b6b360cad",
              "name": "wordpressApplicationPassword",
              "type": "string",
              "value": "your 4app pass word"
            },
            {
              "id": "5aee5eef-9ad2-4dfb-a63f-1b5228c47e31",
              "name": "contentTitle",
              "type": "string",
              "value": "This is a demo title"
            },
            {
              "id": "2abf516c-2910-4cd0-89fe-119cd0e616c8",
              "name": "contentText",
              "type": "string",
              "value": "This is the main text."
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "157b9656-5d90-44f4-aa3c-1285cda698d8",
      "name": "ManualTrigger",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -580,
        -700
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "1d2f6916-e5bd-497b-9843-8bb5a48e9866",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -420,
        -820
      ],
      "parameters": {
        "width": 180,
        "height": 360,
        "content": "## Settings"
      },
      "typeVersion": 1
    },
    {
      "id": "1306446a-f628-44ba-9ca5-751b634bd5dd",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        140,
        -820
      ],
      "parameters": {
        "color": 5,
        "width": 720,
        "height": 360,
        "content": "## Response Handling"
      },
      "typeVersion": 1
    },
    {
      "id": "ec3006aa-34c8-4522-8c37-980f68f168b5",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -220,
        -820
      ],
      "parameters": {
        "color": 3,
        "width": 340,
        "height": 360,
        "content": "## Request Sending"
      },
      "typeVersion": 1
    },
    {
      "id": "bc918075-bea5-4a27-90d9-874b0917a958",
      "name": "Success",
      "type": "n8n-nodes-base.noOp",
      "position": [
        660,
        -780
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "3ea541b7-080e-4694-b865-d7d04f69ea0c",
      "name": "Error",
      "type": "n8n-nodes-base.noOp",
      "position": [
        660,
        -620
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "457c0687-ac1d-49e2-b434-6e1de9acb3a3",
      "name": "PrepareXML",
      "type": "n8n-nodes-base.code",
      "notes": "(request payload, escaping)",
      "position": [
        -180,
        -700
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const input = $json; // If other nodes are in between: $('Settings').item.json;

const username = input.wordpressUsername;
const password = input.wordpressApplicationPassword;
const title = input.contentTitle;
const text = input.contentText;

const blogId = 0;
const published = 1; // 0 = draft, 1 = published


// Helper function to escape XML special characters
function escapeXml(unsafe) {
  return unsafe.replace(/[<>&'\"]/g, (c) => {
    switch (c) {
      case '<': return '&lt;';
      case '>': return '&gt;';
      case '&': return '&amp;';
      case '\'': return '&apos;';
      case '\"': return '&quot;';
      default: return c;
    }
  });
}

// Your actual post text, which may contain characters needing escaping
const titleEscaped = escapeXml(title);
const textEscaped = escapeXml(text);

// Build the XML payload
const xmlData = `<?xml version=\"1.0\"?>
<methodCall>
  <methodName>wp.newPost</methodName>
  <params>
    <param>
      <value><string>${blogId}</string></value>
    </param>
    <param>
      <value><string>${username}</string></value>
    </param>
    <param>
      <value><string>${password}</string></value>
    </param>
    <param>
      <value>
        <struct>
          <member>
            <name>post_title</name>
            <value><string>${titleEscaped}</string></value>
          </member>
          <member>
            <name>post_content</name>
            <value><string>${textEscaped}</string></value>
          </member>
        </struct>
      </value>
    </param>
    <param>
      <value><boolean>${published}</boolean></value>
    </param>
  </params>
</methodCall>`;


// Add a new field called 'myNewField' to the JSON of the item
$input.item.json.xmlRequestBody = xmlData;

return $input.item;"
      },
      "notesInFlow": true,
      "typeVersion": 2
    },
    {
      "id": "3f29f3ed-f7ae-475b-bce3-04d3eeeacee9",
      "name": "PostRequest",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -20,
        -700
      ],
      "parameters": {
        "url": "=https://{{ $('Settings').item.json.wordpressUrl }}/xmlrpc.php",
        "body": "={{ $json.xmlRequestBody }}",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "contentType": "raw",
        "sendHeaders": true,
        "rawContentType": "text/xml",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "text/xml"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "5f320d9b-8aa9-4d13-83db-86acaf444e92",
      "name": "IsSuccessful",
      "type": "n8n-nodes-base.if",
      "position": [
        420,
        -700
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "815d85a1-8f91-4338-977f-503f02c53ea2",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.methodResponse.params.param.value }}",
              "rightValue": ""
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "3a37d19a-12d3-474b-840f-c09342eecca9",
      "name": "HandleResponse",
      "type": "n8n-nodes-base.xml",
      "position": [
        220,
        -700
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "78f90dc5-6209-4db0-b6c6-9f2324488605",
  "connections": {
    "Settings": {
      "main": [
        [
          {
            "node": "PrepareXML",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PrepareXML": {
      "main": [
        [
          {
            "node": "PostRequest",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PostRequest": {
      "main": [
        [
          {
            "node": "HandleResponse",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IsSuccessful": {
      "main": [
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ManualTrigger": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HandleResponse": {
      "main": [
        [
          {
            "node": "IsSuccessful",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

功能特点

  • 自动检测新邮件
  • AI智能内容分析
  • 自定义分类规则
  • 批量处理能力
  • 详细的处理日志

技术分析

节点类型及作用

  • Set
  • Manualtrigger
  • Stickynote
  • Noop
  • Code

复杂度评估

配置难度:
★★★★☆
维护难度:
★★☆☆☆
扩展性:
★★★★☆

实施指南

前置条件

  • 有效的Gmail账户
  • n8n平台访问权限
  • Google API凭证
  • AI分类服务订阅

配置步骤

  1. 在n8n中导入工作流JSON文件
  2. 配置Gmail节点的认证信息
  3. 设置AI分类器的API密钥
  4. 自定义分类规则和标签映射
  5. 测试工作流执行
  6. 配置定时触发器(可选)

关键参数

参数名称 默认值 说明
maxEmails 50 单次处理的最大邮件数量
confidenceThreshold 0.8 分类置信度阈值
autoLabel true 是否自动添加标签

最佳实践

优化建议

  • 定期更新AI分类模型以提高准确性
  • 根据邮件量调整处理批次大小
  • 设置合理的分类置信度阈值
  • 定期清理过期的分类规则

安全注意事项

  • 妥善保管API密钥和认证信息
  • 限制工作流的访问权限
  • 定期审查处理日志
  • 启用双因素认证保护Gmail账户

性能优化

  • 使用增量处理减少重复工作
  • 缓存频繁访问的数据
  • 并行处理多个邮件分类任务
  • 监控系统资源使用情况

故障排除

常见问题

邮件未被正确分类

检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。

Gmail认证失败

确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。

调试技巧

  • 启用详细日志记录查看每个步骤的执行情况
  • 使用测试邮件验证分类逻辑
  • 检查网络连接和API服务状态
  • 逐步执行工作流定位问题节点

错误处理

工作流包含以下错误处理机制:

  • 网络超时自动重试(最多3次)
  • API错误记录和告警
  • 处理失败邮件的隔离机制
  • 异常情况下的回滚操作